package net.handler
{
	import fgame.GameState;
	import ui.UIManager;
	import fgame.controller.Controller;
	import fgame.scene.SceneManager;
	import fgame.unit.Actor3D;
	
	import flash.events.Event;
	import flash.utils.getTimer;
	
	import model.ConfigUnitHelper;
	import model.HeroDataAttribute;
	import model.World;
	import model.config.Unit;
	import model.user.User;
	
	import net.msg.SvrMsg;
	
	import ui.login.LoginView;
	

	public class Handler
	{
		public function Handler()
		{
			
		}
		
		/**
		 * 创建角色
		 */
		public function createCharactar(msg:Object):void
		{
			var p:LoginView=new LoginView();
			p.initialize();
			p.addEventListener("createSure",onCreated);
			GameState.inst.root.addChild(p);
		}

		
		private function onCreated(e:Event):void
		{
			var p:LoginView=e.target as LoginView;
			var hreoName:String=p.getHeroName();
			var unitID:String=p.unitID;
			p.dispose();
			
			var unit:Unit=ConfigUnitHelper.getHeroConfig(unitID);
			Debug.traceMe(unit,"unit");
			unit=unit.clone();
			unit["disName"]=hreoName;
			var obj:SvrMsg=new SvrMsg("RoomAu",NaN,{"info":unit,"heroName":hreoName});
			MsgHelper.sendMsgToS(obj);
			Debug.traceMe(unit,"unit.clone()");
			
		}
		
		public function validated(msg:Object):void
		{
			if(msg.arg)
			{
				GameState.inst.viewer3D.visible=false;
				var unit:Unit=new Unit();
				unit.copy(msg.arg.info);
				User.heroData=unit;
				Debug.traceMe(unit,"返回的数据");
				UIManager.inst.showHall();
				var smsg:SvrMsg=new SvrMsg("Pub",10);//得到房间列表
				MsgHelper.sendMsgToS(smsg);
			}
			
		}
		
		
		/**
		 * 处理act消息
		 */
		public function act(msg:Object):void
		{
			

			var args:Object=msg.arg;
			var name:String=args.name;
			var types:int=args.type;
			var par:*=args.args;
			var sm:SceneManager=GameState.inst.sceneManager;
			var actor:Actor3D=sm.getActorByName(name);

			if(actor)
			{
				actor.processAct(types,par);
			}
		}
		
		/**
		 * 接收提示消息
		 */
		public function showTips(obj:Object):void
		{
			UIManager.inst.showTips(obj.arg);
//			trace(obj.arg);	
		}
		
		/**
		 * 接收网络ping包
		 */
		public function ping(obj:Object):void
		{
			if(isPingSend)
			{
				reveicePingTime=getTimer();
				MsgHelper.netDelay=reveicePingTime-sendPingTime;
				isPingSend=false;
				var act:Actor3D=Controller.instance.target;
				if(act)
					act.netDelay=MsgHelper.netDelay;
				
				var smsg:SvrMsg=new SvrMsg("Room",5,HeroDataAttribute.NET_DELAY);
				MsgHelper.sendMsgToS(smsg);
//				Debug.traceObj("网络延时:"+MsgHelper.netDelay,"ping");	
			}
		}
		
		/**
		 * 发送ping包
		 */
		public static function sendPing():void
		{
			if(!isPingSend)
			{
				sendPingTime=getTimer();
				var obj:SvrMsg=new SvrMsg("Pub",0,{"delay":MsgHelper.netDelay});
				MsgHelper.sendMsgToS(obj);
				isPingSend=true;
			}
		}
		
		private static var sendPingTime:int=1;
		private static var reveicePingTime:int=1;
		/** ping命令发送中  */
		public static var isPingSend:Boolean=false;

	}
}